package demo.DataStrucAndAlgo.Search;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/7/27-07-27-19:56
 * @Description：demo.DataStrucAndAlgo.Search    //顺序查找
 */
/*
顺序查找
    遍历数据，匹配查找内容，找到返回其索引，没找到返回-1
 */
public class seqSearch {
    public static void main(String[] args) {
        int[] data = {1,2,3,4,5,6};
        System.out.println(seq_search(data,5));
    }

    /**
     * 顺序查找
     * @param data    查找的数据数组
     * @param num     需要查找的数据
     * @return        查找到返回其索引，没找到返回-1
     */
    public static int seq_search(int[] data, int num) {
        int index = -1;
        for (int i = 0; i < data.length; i++) {
            if(data[i] == num){
                index = i;
                break;
            }
        }
        return index;
    }
}
